package com.zxc.reggie.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLIntegrityConstraintViolationException;

@Slf4j
@ResponseBody
@ControllerAdvice(annotations = {RestController.class, RequestMapping.class})
public class GlobalExceptionHandel {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> exceptionHandel(SQLIntegrityConstraintViolationException ex) {
        String message = ex.getMessage();
        log.error(ex.getMessage());

        if (message.contains("Duplicate")) {
            String[] strings = message.split(" ");
            message = strings[2] + "用户名重复了";
            return R.error(message);
        }
        return R.error("出错了");
    }

    @ExceptionHandler(CustomException.class)
    public R<String> exceptionHandel(CustomException ex) {
        String message = ex.getMessage();
        log.error(message);
        return R.error(message);
    }
}
